chinamox has asked for the wisdom of the Perl Monks concerning the following question:

Brothers I have sinned - but I know not how!

I am trying to use File::Basename to get the file name from a path passed into the program via $ARGV1.

$org_fn = $ARGV[1]; ($file,$dir,$bak) = fileparse("/tmp/file.bak", qr/~+$/, qr/\.(bak|orig|save)/) print "$file \n";

The output comes out as either 'path/path/path/file.ext' or '$ARGV' (when I used single quotes, which I see is wrong now). While I dug the regex out of O'reilly's Perl Cookbook, I now think I am asking fileparse() to do something with $ARGV that is either impossible or even sinful. Is this correct? How can get the filename I want?

Thank you,

-mox

Replies are listed 'Best First'.
Re: Using File::Basename and $ARGV[]
by liverpole (Monsignor) on Oct 23, 2006 at 15:09 UTC
    Hi chinamox,

    I think what you want is simply:

    use strict; use warnings; use File::Basename; $org_fn = $ARGV[1]; my $base = basename $org_fn; print "Basename is $base\n";

    Note that File::Basename is a great way to get the name of the script itself, too.

    I usually do this:

    my $iam = basename $0; my $syntax = " syntax: $iam ... ";

    and then don't have to worry about changing the program name; the syntax message stays correct.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      Thank you, that is exactly what I wanted! I figured I was trying a little to hard and just needed nudge in the right direction. Also thank you for the excellent tip on using File::Basename to fixing the name of the script.

      -mox
Re: Using File::Basename and $ARGV[]
by davorg (Chancellor) on Oct 23, 2006 at 15:10 UTC

    What input are you giving? And what output are you expecting? Oh, and what operating system is this running on?

    (when I used single quotes, which I see is wrong now)

    Assuming you mean fileparse("$org_fn", ...), there are no quotes needed there at all. See the FAQ What's wrong with always quoting "$vars"?.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Thank you for the link. You were right, I needed to dump the quotes and keep it simple. I am trying to run this on a *INX box and will include that information in my future posts.

      Best

      -mox
        You were right, I needed to dump the quotes and keep it simple.

        Just to be picky... you didn't need to dump the quotes, but you didn't need them in the first place. That is, using them doesn't make for a syntax or semantics error. Simply, it won't do anything (unless of course you've overloaded double quotes) and it will have some disadvantages as evidenced in the FAQ entry you've been pointed to.