Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Getting a base name

by Anonymous Monk
on Jul 20, 2005 at 08:20 UTC ( #476409=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Why this code:
perl -e ' $fname = 'test.txt'; $base = (split(/./,$fname))[0]; print "$base\n";'
Doesn't prints me:
Thanks a million again.

Replies are listed 'Best First'.
Re: Getting a base name
by Iron (Scribe) on Jul 20, 2005 at 08:30 UTC
    You should use

    $base = (split(/\./,$fname))[0];

    and better solution is File::Basename
Re: Getting a base name
by Smylers (Pilgrim) on Jul 20, 2005 at 08:30 UTC

    Put a backslash before the dot.


Re: Getting a base name
by Zaxo (Archbishop) on Jul 20, 2005 at 08:49 UTC

    Iron wisely suggests File::Basename. Here's how it goes,

    use File::Basename; my $filename = "../../path/to/test.txt"; my $basename = basename( $filename, '.txt', '.dat'); print $basename, $/; __END__ test
    If you omit the extension list arguments, or if $filename's extension is not in the list, basename() will return the basename with extension.

    F::B also offers the functions dirname(), which extracts just the directory part, and fileparse(), which returns a list of ($basename, $dirname, $extension)

    After Compline,

Re: Getting a base name
by polettix (Vicar) on Jul 20, 2005 at 08:33 UTC
    You're using single quotes inside single quotes; moreover, the dot is a special character in a regexp, you have to escape it:
    perl -e ' $fname = "test.txt"; # Double quotes! $base = (split(/\./,$fname))[0]; # Escape dot! print "$base\n";'

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Getting a base name
by Tanalis (Curate) on Jul 20, 2005 at 08:31 UTC
    You need to escape the '.' in your split:
    $base = ( split(/\./,$fname) )[0];
    Adding -Mstrict and -w (always a good idea), the following works fine for me:
    perl -Mstrict -w -e ' my $fname = "test.txt"; my $base = ( split(/\./,$fname) )[0]; print "$base\n";'
    Update: And, as others have mentioned, you have single quotes inside single quotes, too :)

    Hope that helps ..

Re: Getting a base name
by gube (Parson) on Jul 20, 2005 at 08:33 UTC
    $fname = 'test.txt'; $base = (split/\./, $fname)[0]; print "$base\n";
Re: Getting a base name
by reasonablekeith (Deacon) on Jul 20, 2005 at 08:32 UTC
    Your dot is matching anything, you wanted to match a dot explicitly
    $base = (split(/\./,$fname))[0];
    UPDATE: Too slow, that's probably enough replies now :-)
    my name's not Keith, and I'm not reasonable.
Re: Getting a base name
by SimonClinch (Deacon) on Jul 20, 2005 at 08:40 UTC
    The delimiter for split is being parsed locally as a regular expression wildcard which is not very useful in this context. Either escaping it with backslash /\./ or enclosing in single quotes '.' will do the trick, although the latter will be more difficult in this case given that the whole program is also enclosed in single quotes.

    One world, one people

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://476409]
Approved by Tanalis
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-02-20 00:15 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (266 votes). Check out past polls.