How safe is $0?

How safe is $0?

am 24.08.2007 12:00:33 von Bill H

If I use $0 to determine the name of the script that is running is it
safe to assume that it will not change within the script (as long as I
never intentionally try to change it? The way I am using it is setting
certain messages based on the the script. Somthing like this:

if ($0 eq "foo.pl")
{
$blah = "foo";
}
if ($0 eq "bar.pl")
{
$blah = "bar";
}

I tried to find this in the perldocs but didn't.

Bill H

Re: How safe is $0?

am 24.08.2007 13:16:10 von anno4000

Bill H wrote in comp.lang.perl.misc:
> If I use $0 to determine the name of the script that is running is it
> safe to assume that it will not change within the script (as long as I
> never intentionally try to change it? The way I am using it is setting
> certain messages based on the the script. Somthing like this:
>
> if ($0 eq "foo.pl")
> {
> $blah = "foo";
> }
> if ($0 eq "bar.pl")
> {
> $blah = "bar";
> }

Well, $0 is a writable package variable, so in principle anyone
can change it. To do so in a module would have to be considred
bad style, but that's no guarantee it won't happen. To be safe
you can but the code that uses $0 in a BEGIN block in the main
script that happens before any "use" statements:

my $blah;
BEGIN { ( $blah = $0) =~ s/\.pl$// }

use ...;

Anno

Re: How safe is $0?

am 24.08.2007 16:49:10 von xhoster

Bill H wrote:
> If I use $0 to determine the name of the script that is running is it
> safe to assume that it will not change within the script (as long as I
> never intentionally try to change it?

That is probably fairly safe to assume, but why assume? I can't think of
Perl's internals mucking with it without being asked to. Some weird module
might want to.


> The way I am using it is setting
> certain messages based on the the script. Somthing like this:
>
> if ($0 eq "foo.pl")
> {
> $blah = "foo";
> }
> if ($0 eq "bar.pl")
> {
> $blah = "bar";
> }

Why not just put "my $orig_0=$0" near the top of the script? Then
you don't have to worry about either Perl's internals or other modules
changing it, plus it frees you (or your successor) to change the real $0
in the future if you ever feel the need to use its status-indicating
feature.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: How safe is $0?

am 24.08.2007 16:52:49 von Michele Dondi

On 24 Aug 2007 14:49:10 GMT, xhoster@gmail.com wrote:

>Why not just put "my $orig_0=$0" near the top of the script? Then
>you don't have to worry about either Perl's internals or other modules
>changing it, plus it frees you (or your successor) to change the real $0
>in the future if you ever feel the need to use its status-indicating
>feature.

Huh?!? "near the top"? As Anno correctly pointed out, to be really
sure it needs to be in a BEGIN block before other BEGIN blocks and
use's.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: How safe is $0?

am 24.08.2007 18:05:38 von xhoster

Michele Dondi wrote:
> On 24 Aug 2007 14:49:10 GMT, xhoster@gmail.com wrote:
>
> >Why not just put "my $orig_0=$0" near the top of the script? Then
> >you don't have to worry about either Perl's internals or other modules
> >changing it, plus it frees you (or your successor) to change the real $0
> >in the future if you ever feel the need to use its status-indicating
> >feature.
>
> Huh?!? "near the top"? As Anno correctly pointed out, to be really
> sure it needs to be in a BEGIN block before other BEGIN blocks and
> use's.

Sure, if you are afraid someone will assign to $0 during compilation.
That seems even less likely than assigning to it during run-time. But
better safe than sorry, I guess.

my $orig_0; BEGIN {$orig_0=$0};

It would be nice if there was some way to "my" something but causing
it to get defined in the lexical scope one level up (or, one level "out" I
suppose, rather than up).


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

Re: How safe is $0?

am 24.08.2007 19:00:25 von Michele Dondi

On 24 Aug 2007 16:05:38 GMT, xhoster@gmail.com wrote:

>Michele Dondi wrote:
>> On 24 Aug 2007 14:49:10 GMT, xhoster@gmail.com wrote:
>>
>> >Why not just put "my $orig_0=$0" near the top of the script? Then
>> >you don't have to worry about either Perl's internals or other modules

^^^^^^^^^^^^^^^^

^^^^^^^^^^^^^^^^

>> >changing it, plus it frees you (or your successor) to change the real $0
>> >in the future if you ever feel the need to use its status-indicating
>> >feature.
>>
>> Huh?!? "near the top"? As Anno correctly pointed out, to be really
>> sure it needs to be in a BEGIN block before other BEGIN blocks and
>> use's.
>
>Sure, if you are afraid someone will assign to $0 during compilation.
>That seems even less likely than assigning to it during run-time. But
>better safe than sorry, I guess.

Since you wrote "or other modules", it was just to be precise.

>my $orig_0; BEGIN {$orig_0=$0};
>
>It would be nice if there was some way to "my" something but causing
>it to get defined in the lexical scope one level up (or, one level "out" I
>suppose, rather than up).

I *thought* that Perl 6's OUTER:: namespace could serve this purpose,
but this doesn't seem to be the case, at least as of the pugs I have
(this is not a definitive proof):

pugs> {my $OUTER::foo=10}
Internal error while running expression:
***
Unexpected "::foo"
expecting word character, "?", "!", trait, "=", infix assignment,
term postf
ix, operator or "}"
at line 1, column 11
pugs> my $foo; {$OUTER::foo=10}
10
pugs> say $foo;
10


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Re: How safe is $0?

am 24.08.2007 19:28:29 von ansok

In article <1187949633.488453.272370@q3g2000prf.googlegroups.com>,
Bill H wrote:
>If I use $0 to determine the name of the script that is running is it
>safe to assume that it will not change within the script (as long as I
>never intentionally try to change it? The way I am using it is setting
>certain messages based on the the script. Somthing like this:
>
>if ($0 eq "foo.pl")
>{
>$blah = "foo";
>}
>if ($0 eq "bar.pl")
>{
>$blah = "bar";
>}

Not directly related to your question, but bear in mind that it is not
specified whether $0 will be just the script name, or might contain
directory path information. In my experience, Unix-based systems usually
have $0 containing whatever was entered by the user (just "foo.pl" if the
script was found through $PATH, or the path entered by the user), and
Windows has $0 contain a full path to the script. So you might want
to use basename() (from File::Basename) on the contents of $0.

Also, knowlegable users can set $0 to anything they want to at the time
that your script is started, before any of your code runs. So if this
is critical to your script's functionality (or security), think twice.

That said, I think it pretty safe to say that no Perl core or widely-used
modules are likely to change $0 at all.

Gary Ansok
--
Any attempt to brew coffee with a teapot should result in the error code
"418 I'm a teapot". The resulting entity body MAY be short and stout.
-- RFC 2324, Hyper Text Coffee Pot Control Protocol (HTCPCP)/1.0

Re: How safe is $0?

am 24.08.2007 22:47:55 von Bill H

On Aug 24, 1:28 pm, an...@alumni.caltech.edu (Gary E. Ansok) wrote:
> In article <1187949633.488453.272...@q3g2000prf.googlegroups.com>,
> Bill H wrote:
>
> >If I use $0 to determine the name of the script that is running is it
> >safe to assume that it will not change within the script (as long as I
> >never intentionally try to change it? The way I am using it is setting
> >certain messages based on the the script. Somthing like this:
>
> >if ($0 eq "foo.pl")
> >{
> >$blah = "foo";
> >}
> >if ($0 eq "bar.pl")
> >{
> >$blah = "bar";
> >}
>
> Not directly related to your question, but bear in mind that it is not
> specified whether $0 will be just the script name, or might contain
> directory path information. In my experience, Unix-based systems usually
> have $0 containing whatever was entered by the user (just "foo.pl" if the
> script was found through $PATH, or the path entered by the user), and
> Windows has $0 contain a full path to the script. So you might want
> to use basename() (from File::Basename) on the contents of $0.
>
> Also, knowlegable users can set $0 to anything they want to at the time
> that your script is started, before any of your code runs. So if this
> is critical to your script's functionality (or security), think twice.
>
> That said, I think it pretty safe to say that no Perl core or widely-used
> modules are likely to change $0 at all.
>
> Gary Ansok
> --
> Any attempt to brew coffee with a teapot should result in the error code
> "418 I'm a teapot". The resulting entity body MAY be short and stout.
> -- RFC 2324, Hyper Text Coffee Pot Control Protocol (HTCPCP)/1.0

Gary

I learned this today. On my windows system testing $0 was great, on
the linux webserver, it was the full path. So the easiest thing was to
just to use index and cover both bases (ie index($0,"for.pl")).

Re: How safe is $0?

am 25.08.2007 12:23:11 von Michele Dondi

On Fri, 24 Aug 2007 13:47:55 -0700, Bill H wrote:

>I learned this today. On my windows system testing $0 was great, on
>the linux webserver, it was the full path. So the easiest thing was to
>just to use index and cover both bases (ie index($0,"for.pl")).

The easiest and more reliable way would have been to use basename() as
has already been suggested to you. It is unlikely, but what if you
have to do with /path/to/for.pl.for.pl?


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,