Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Different behavior when parsing $0 on Solaris and Win32

by dirac (Beadle)
on Sep 28, 2011 at 12:37 UTC ( #928286=perlquestion: print w/ replies, xml ) Need Help??
dirac has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

I have tried to extract the name of the running script without his extension, to create a new file with same name but with ".log" at end.

On win32 it works fine, but on Solaris I get only ".log".

This is the code:

#!/usr/local/bin/perl -w # my_script_name.pl use strict; my $log_file = (split /\./, $0)[0] . '.log'; print $log_file, "\n";

On Win32 the output is:

my_script_name.log

On Solaris with perl, v5.8.8 built for sun4-solaris

.log

Any suggestions?

Thanks

Comment on Different behavior when parsing $0 on Solaris and Win32
Select or Download Code
Re: Different behavior when parsing $0 on Solaris and Win32
by Corion (Pope) on Sep 28, 2011 at 12:46 UTC

    Works for me with

    This is perl, v5.8.4 built for i86pc-solaris-64int (with 32 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall

    But note that you are not really taking the script name, you are also potentially taking the whole path to the script name. And you are not replacing the end of the script name by .log but you are replacing the complete part after the first dot in the script path and replace it by .log. I would do it this way:

    (my $logname = $0) =~ /\.pl$/.log/;
      you are replacing the complete part after the first dot in the script path and replace it by .log
      Good point. And here is one way of calling the script to reproduce the OP's result:
      $ ./my_script_name.pl .log
      Perhaps the OP has invoked the script differently on the 2 OS's.

        yes, this is exactly what happens, even setting PATH with the current dir and invoking with:

        $ my_script_name.pl

        The value of $0 is still the same:

        ./my_script_name.pl

      With this, work fine (note the 's/')

      (my $log_file = $0 )=~ s/pl$/log/;
Re: Different behavior when parsing $0 on Solaris and Win32
by toolic (Chancellor) on Sep 28, 2011 at 12:49 UTC
    I can't attempt to reproduce your results because I do not have access to those OS's. But, you could investigate further by showing us the contents of $0:
    use strict; use warnings; print "script = >>>$0<<<\n"; my $log_file = (split /\./, $0)[0] . '.log'; print $log_file, "\n";
    Basic debugging checklist
Reaped: Re: Different behavior when parsing $0 on Solaris and Win32
by NodeReaper (Curate) on Sep 28, 2011 at 12:53 UTC
Re: Different behavior when parsing $0 on Solaris and Win32
by liverpole (Monsignor) on Sep 28, 2011 at 12:55 UTC
    Hi dirac,

    When a script doesn't work as expected, you need to debug it. To aid in that venture, be verbose. Don't get fancy and try to do everything in a single line (eg. my $log_file = (split /\./, $0)[0] . '.log';). Rather, do the minimum possible in each step, and display all intermediate results, such as:

    #!/usr/local/bin/perl -w # my_script_name.pl use strict; my $arg0 = $0; print "Debug> Before split -- arg0 = '$arg0'\n"; my @words = split /\./, $0; my $count = @words; print "Debug> After split -- $count words: [@words]\n"; my $base = $words[0]; print "Debug> First word (word[0]) = '$base'\n"; my $log_file = $base . '.log'; print "Debug> Full logfile name = '$log_file'\n";

    You should never find yourself saying "I know the contents of the variable should be X at this point". Rather, "I think the contents of the variable should be X, so I'll print them out to be sure..."


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
Re: Different behavior when parsing $0 on Solaris and Win32
by TomDLux (Vicar) on Sep 28, 2011 at 18:04 UTC

    What happens when you run the script under the debugger? The -d option is there for a reason.

    Try File::Basename::fileparse().

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Reaped: Re: Different behavior when parsing $0 on Solaris and Win32
by NodeReaper (Curate) on Sep 29, 2011 at 13:16 UTC
Reaped: Re: Different behavior when parsing $0 on Solaris and Win32
by NodeReaper (Curate) on Sep 30, 2011 at 13:16 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://928286]
Front-paged by davies
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2014-11-29 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls