I wasn't aware about Path::Class. I was suprised upon reading the docs that it provides two distinct objects without a base implementing public interfaces for common behaviours. I would just say,
$ perl -MFile::Spec -wle 'my $path = shift; my $last = (File::Spec->sp +litpath($path))[-1]; print $last' /etc/httpd/conf conf $ perl -MFile::Spec -wle 'my $path = shift; my $last = (File::Spec->sp +litpath($path))[-1]; print $last' /etc/httpd/conf/httpd.conf httpd.conf
I think you can just pass the object to File::Spec::splitpath() function because the class implements string overloading method. So, you don't have to care whether what you pass is ::File or ::Dir object.
$ perl -MPath::Class -wle 'my $path = shift; my $obj = file($path); pr +int +(File::Spec->splitpath($obj))[-1]' /etc/httpd/conf/httpd.conf httpd.conf $ perl -MPath::Class -wle 'my $path = shift; my $obj = dir($path); pri +nt +(File::Spec->splitpath($obj))[-1]' /etc/httpd/conf conf
Of course, you still have to know what object you want to create in the first place.

Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!


In reply to Re: get the "last part" of a Path::Class object by naikonta
in thread get the "last part" of a Path::Class object by thepler

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.