From: ccc31807 on
use constant DIR => 'log_files';
mkdir DIR unless -e DIR;

This places a subdirectory in my current working directory named
log_files, creating it if it doesn't exist.

print_log();
sub print_log
{
open LOG, '>', "DIR/log.csv" or die "Cannot open LOG, $!";
...
}

This throws a DOES NOT EXIST error. However,

print_log(DIR);
sub print_log
{
my $dir = shift;
open LOG, '>', "$dir/log.csv" or die "Cannot open LOG, $!";
...
}

works just fine. What am I missing?

Thanks, CC.
From: Uri Guttman on
>>>>> "c" == ccc31807 <cartercc(a)gmail.com> writes:

c> use constant DIR => 'log_files';
c> mkdir DIR unless -e DIR;

c> This places a subdirectory in my current working directory named
c> log_files, creating it if it doesn't exist.

c> print_log();
c> sub print_log
c> {
c> open LOG, '>', "DIR/log.csv" or die "Cannot open LOG, $!";
c> ...
c> }

c> This throws a DOES NOT EXIST error. However,

perl constants are really subs with a prototype of no arguments. they
are converted at compile to their value (and constant folded if
possible).

"DIR" won't work because subs don't directly interpolate. you can work
around that with DIR . '/log.csv' or the @{[DIR]} interpolation trick.

another way is to use the ReadOnly module which declares regular
variables which can't be modified. then you can use $DIR in either situation.

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
From: ccc31807 on
On Apr 27, 5:35 pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
> perl constants are really subs with a prototype of no arguments. they
> are converted at compile to their value (and constant folded if
> possible).

Uri, thanks for your explanation.

I would then assume that
- use constant DIR => 'log_files';
would be more or less equivalent to
- sub DIR { return 'log_files'; }

Is this assumption correct, more or less?

Thanks, CC.
From: Tad McClellan on
ccc31807 <cartercc(a)gmail.com> wrote:

> use constant DIR => 'log_files';

> open LOG, '>', "DIR/log.csv" or die "Cannot open LOG, $!";


perldoc constant

Constants defined using this module cannot be interpolated
into strings like variables.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.
From: Uri Guttman on
>>>>> "c" == ccc31807 <cartercc(a)gmail.com> writes:

c> On Apr 27, 5:35�pm, "Uri Guttman" <u...(a)StemSystems.com> wrote:
>> perl constants are really subs with a prototype of no arguments. they
>> are converted at compile to their value (and constant folded if
>> possible).

c> Uri, thanks for your explanation.

c> I would then assume that
c> - use constant DIR => 'log_files';
c> would be more or less equivalent to
c> - sub DIR { return 'log_files'; }

sub DIR() { 'log_files' }

the important difference is the prototype of () so it can be compile
time converted to a constant. the return isn't needed (i dunno if it
affects it becoming a proper constant but i doubt it).

uri

--
Uri Guttman ------ uri(a)stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------