On 09/25/2017 06:39 AM, Timo Paulssen wrote:

Second itteration:

<code>
#!/usr/bin/env perl6

say "Full file name <$?FILE>";

my $IAm;
my $IAmFrom;

$?FILE ~~ m|(.*\/)(.*)|;
$IAmFrom = $0;
$IAm     = $1;
say "IAm            <$IAm>";
say "IAmFrom        <$IAmFrom>";
</code>

Please don't use string functions on paths; doing it like this is liable
to break when used on another OS like windows where paths are separated
with \ instead of /

What you're doing here is exactly what .IO.basename and .IO.dirname are for.

     https://docs.perl6.org/type/IO::Path#method_basename
     https://docs.perl6.org/type/IO::Path#method_dirname



Hi Timo,

There is another operating system other than Linux?  I
had heard rumors of that: it is slow, expensive, crashes
all the time.  But I thought is was only a story to frighten
little children and young programmers.   SAY IT ISN'T
TRUE!!!   :-)

The misunderstanding on what was an exercise on me learning subs
and matches, turned into a wonderful example for my keepers file
on OS name and path.  Thank you!

Oh ya, and your method doesn't comes out with that annoying "/./"!

-T

This is my keeper file:

<perl6.program.name.txt>
perl 6: program name and path:


    https://docs.perl6.org/type/IO::Path#method_basename
    https://docs.perl6.org/type/IO::Path#method_dirname
    https://docs.perl6.org/language/variables#Compile-time_variables


my $WhoAmI = $*PROGRAM-NAME;
( my $IAm =~ $?FILE ) ~~ s|.*"/"||;
my $IAmFrom = $?FILE.IO.dirname;
my $IAm     = $?FILE.IO.basename;



program path and name:

<code>
#!/usr/bin/env perl6

say "Full file name <$?FILE>";

my $IAm;
my $IAmFrom;

$?FILE ~~ m|(.*\/)(.*)|;
$IAmFrom = $0;
$IAm     = $1;
say "IAm            <$IAm>";
say "IAmFrom        <$IAmFrom>\n";

say "and the OS agnostic way:";
$IAmFrom = $?FILE.IO.dirname;
$IAm     = $?FILE.IO.basename;
say "IAm            <$IAm>";
say "IAmFrom        <$IAmFrom>";

</code>

$ FileTest.pl6
Full file name </home/linuxutil/./FileTest.pl6>
IAm            <FileTest.pl6>
IAmFrom        </home/linuxutil/./>

and the OS agnostic way:
IAm            <FileTest.pl6>
IAmFrom        </home/linuxutil/.>


$ /home/linuxutil/FileTest.pl6
Full file name </home/linuxutil/FileTest.pl6>
IAm            <FileTest.pl6>
IAmFrom        </home/linuxutil/>

and the OS agnostic way:
IAm            <FileTest.pl6>
IAmFrom        </home/linuxutil>

Reply via email to