Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Different behavior when parsing $0 on Solaris and Win32

by dirac (Beadle)
on Sep 28, 2011 at 12:37 UTC ( [id://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

Replies are listed 'Best First'.
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 Corion (Patriarch) 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 (Bishop) 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
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found