Re: utilities in perl

Discussion in 'Perl Misc' started by George Mpouras, Sep 21, 2013.

  1. # you may like it

    #!/usr/bin/perl
    use strict;
    use warnings;
    use feature 'say';
    my $dir = exists $ARGV[0] && -d $ARGV[0] ? $ARGV[0] : './';
    foreach(<$dir/*>){say "$_ is ", -d $_ ? 'directory':'file'}
    George Mpouras, Sep 21, 2013
    #1
    1. Advertising

  2. George Mpouras

    J. Gleixner Guest

    On 09/24/13 19:46, Cal Dershowitz wrote:

    > [...] For the purposes of the template, I basically
    > want to hard code the directory at this point.


    Or.. look at using Find::Bin. That way you could run
    /home/blah/get_file1.pl .. instead of always being in
    /home/blah and having to run ./get_file1.pl.. and the
    templates could be relative to get_file1.pl.
    J. Gleixner, Sep 25, 2013
    #2
    1. Advertising

  3. George Mpouras

    J. Gleixner Guest

    On 09/25/13 12:37, J. Gleixner wrote:
    > On 09/24/13 19:46, Cal Dershowitz wrote:
    >
    >> [...] For the purposes of the template, I basically
    >> want to hard code the directory at this point.

    >
    > Or.. look at using Find::Bin. That way you could run
    > /home/blah/get_file1.pl .. instead of always being in
    > /home/blah and having to run ./get_file1.pl.. and the
    > templates could be relative to get_file1.pl.
    >

    correction.. FindBin not Find::Bin.. sorry.
    J. Gleixner, Sep 25, 2013
    #3
  4. >> Or.. look at using Find::Bin. That way you could run
    >> /home/blah/get_file1.pl .. instead of always being in
    >> /home/blah and having to run ./get_file1.pl.. and the
    >> templates could be relative to get_file1.pl.
    >>

    > correction.. FindBin not Find::Bin.. sorry.



    why the FindBin and not the File::Find ;
    George Mpouras, Sep 25, 2013
    #4
  5. George Mpouras

    $Bill Guest

    On 9/25/2013 15:46, George Mpouras wrote:
    >>> Or.. look at using Find::Bin. That way you could run
    >>> /home/blah/get_file1.pl .. instead of always being in
    >>> /home/blah and having to run ./get_file1.pl.. and the
    >>> templates could be relative to get_file1.pl.
    >>>

    >> correction.. FindBin not Find::Bin.. sorry.

    >
    >
    > why the FindBin and not the File::Find ;


    Why find at all if you're not recursing ?

    opendir/readdir is more straight forward and probably efficient.

    Or a recursive sub using opendir/readdir if you need depth isn't
    all that much more complicated.
    $Bill, Sep 26, 2013
    #5
  6. George Mpouras

    J. Gleixner Guest

    On 09/25/13 17:46, George Mpouras wrote:
    >>> Or.. look at using Find::Bin. That way you could run
    >>> /home/blah/get_file1.pl .. instead of always being in
    >>> /home/blah and having to run ./get_file1.pl.. and the
    >>> templates could be relative to get_file1.pl.
    >>>

    >> correction.. FindBin not Find::Bin.. sorry.

    >
    >
    > why the FindBin and not the File::Find ;
    >
    >


    The part that was meant to address was:

    [...] For the purposes of the template, I basically
    want to hard code the directory at this point.

    Specifically, calling getcwd..

    That's where FindBin comes in handy -- when your templates
    are relative to where your program is located. Many
    times your dev directory/machine might have different
    paths or maybe you want to run it from something else
    and want to call /home/junk/abc.pl instead of having to do
    'cd /home/junk; ./abc.pl' etc. Using FindBin will
    give you a lot of flexibility and you could move/cp everything
    to a different directory ( e.g. for production ) and not
    have to change any code.
    J. Gleixner, Sep 26, 2013
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JWhite

    Perl CGI utilities?

    JWhite, Jul 1, 2008, in forum: Perl Misc
    Replies:
    5
    Views:
    285
    Todd Wade
    Jul 5, 2008
  2. $Bill

    Re: utilities in perl

    $Bill, Sep 21, 2013, in forum: Perl Misc
    Replies:
    4
    Views:
    222
    $Bill
    Sep 22, 2013
  3. Peter J. Holzer

    Re: utilities in perl

    Peter J. Holzer, Sep 21, 2013, in forum: Perl Misc
    Replies:
    14
    Views:
    230
    J├╝rgen Exner
    Sep 25, 2013
  4. Ben Bacarisse

    Re: perl hash utilities

    Ben Bacarisse, Sep 26, 2013, in forum: Perl Misc
    Replies:
    1
    Views:
    169
    Ben Bacarisse
    Sep 26, 2013
  5. Uri Guttman

    Re: perl hash utilities

    Uri Guttman, Sep 26, 2013, in forum: Perl Misc
    Replies:
    2
    Views:
    173
    Charles DeRykus
    Sep 27, 2013
Loading...

Share This Page