Re: utilities in perl

Discussion in 'Perl Misc' started by $Bill, Sep 21, 2013.

  1. $Bill

    $Bill Guest

    On 9/20/2013 17:04, Cal Dershowitz wrote:
    > I'm writing a few utilities in perl and wanted to solicit opinion regarding a couple things.
    >
    > $ pwd
    > /home/fred/Documents/root/pages
    > $ ./hf.pl elmer fudd
    > dir is elmer
    > filetype is fudd
    > elmer
    > fudd
    > $ cat hf.pl
    > #!/usr/bin/perl -w
    > use strict;
    > use 5.010;
    > if ($#ARGV < 1) {
    > print "Needs directory and filetype\n";
    > exit;
    > }
    > my $dir = $ARGV[0];
    > my $filetype = $ARGV[1];
    > print "dir is $dir\n";
    > print "filetype is $filetype\n";
    > print "$dir\n";
    > print "$filetype\n";
    >
    > Q1) Do the ultimate 2 statements effectively pipe the input from stdin to stdout?


    What ultimate 2 statements ? There's no piping involved here.
    You're not using stdin for anything.

    > Q2) Are there inputs for the two values that could cause these values to be different before versus after?


    Not sure what you mean as to different before and after. There
    could be shell problems with the two arguments if they have
    unusual characters in them - eg: spaces and such. You may need
    some quoting to handle that which would depend on the shell you
    are using.

    Not at all sure what you're trying to ask here - might help if
    you stated what type of utilities you were targeting for your
    scripts - they may already exist (unless you just want to practice
    your skills and get some experience with Perl).
    $Bill, Sep 21, 2013
    #1
    1. Advertising

  2. this is what you want
    /hf1.pl /home/fred/Documents/root/pages/terminal/terminal1.pl
    /home/fred/Documents/root/pages/terminal/terminal2.pl
    /home/fred/Documents/root/pages/terminal/terminal23th.pl






    #!/usr/bin/perl
    use strict;
    use warnings;

    foreach (@ARGV)
    {
    my @info = Path_analyzer($_);
    print "upper dir : $info[0]\n";
    print "filename : $info[1]\n";
    print "filebare : $info[2]\n";
    print "extension : $info[3]\n";
    print '-'x30, "\n";
    }




    # 1) dir
    # 2) filename
    # 3) filename without the extension
    # 4) extension
    #
    sub Path_analyzer
    {
    return ('','','','') if (! exists $_[0]) || ($_[0] eq '');
    my ($dir, $file, $body, $ext) = ($_[0],'','','');
    $dir =~s/(\s|\/)*$//;
    ($dir,$file) = $dir =~/^(.*?)([^\/]+)$/;
    $dir =~s/(\s|\/)*$//;

    if ( ( $dir=~/^\s*$/ ) || ( $dir eq '.' ) )
    {
    ( $dir = `/bin/pwd` ) =~s/\s*$//
    }

    if ( -1 == rindex $file, '.' )
    {
    $body=$file; $ext=undef } else { $body = substr $file,0,(rindex $file,'.');
    $ext = substr($file,(rindex $file,'.')+1)
    }

    $ext //= '';
    $dir, $file, $body, $ext
    }
    George Mpouras, Sep 21, 2013
    #2
    1. Advertising

  3. $Bill

    $Bill Guest

    On 9/21/2013 10:13, Cal Dershowitz wrote:
    > #!/usr/bin/perl -w


    See what this one does (I'm using Windows 8 with tcsh here):

    use strict;
    use warnings;

    die "Needs base filename and file ext args" if @ARGV < 2;

    my $base = shift;
    my $ext = shift;
    print "base='$base', ext='$ext'\n";

    opendir DIR, '.' or die "opendir '.': $! ($^W)";
    # grep out base*.ext files
    my @files = grep /^$base\d*\.$ext/, readdir DIR;
    closedir DIR;

    print "Matching files are (@files)\n";

    __END__
    $Bill, Sep 22, 2013
    #3
  4. $Bill

    $Bill Guest

    On 9/21/2013 19:48, $Bill wrote:
    > On 9/21/2013 10:13, Cal Dershowitz wrote:
    >> #!/usr/bin/perl -w

    >
    > See what this one does (I'm using Windows 8 with tcsh here):
    >
    > use strict;
    > use warnings;
    >
    > die "Needs base filename and file ext args" if @ARGV < 2;
    >
    > my $base = shift;
    > my $ext = shift;
    > print "base='$base', ext='$ext'\n";
    >
    > opendir DIR, '.' or die "opendir '.': $! ($^W)";
    > # grep out base\d*.ext files
    > my @files = grep /^$base\d*\.$ext/, readdir DIR;


    Whoops, probably want a $ after $ext to anchor it to end of filename.
    Technically, you could probably make the \d* into a .* to be more
    generic and allow other than numbers, but your situation seems to
    be more specific with numbers/version only.

    > closedir DIR;
    >
    > print "Matching files are (@files)\n";
    >
    > __END__
    $Bill, Sep 22, 2013
    #4
  5. $Bill

    $Bill Guest

    On 9/22/2013 00:58, Cal Dershowitz wrote:
    >
    > Cool, cool, $Bill I believe we're new to each other. I like that you identified yourself with an 'S' which is how some people describe the dollar sign thingy. Perl makes for a very interesting program language for people who aren't afraid to talk about "s underscore" in public. Some people say "sigil."


    The $ actually stands for a '1' and an 'S' superimposed (from my days
    as a computer operator many decades ago - meaning '1st shift' - and
    used to initial run requests in the tiny little initials box).

    > I took your script and will fire it up once I get wireless connectivity for my linux box again. I'm not at that level of maturity where I can be dancing around on the terminal and downloading cpan where I simply cannot.


    Should work on UNIX or Windows.

    > My contention is that I'm guaranteed to be on topic by tuesday. Until then I'd like to consider the question of what perl environment works best on a windows 8 laptop, which is at best a shoulder issue as far as topicality goes. I ask it, because it has directly to do with my pursuit of laziness.


    I prefer Windows 7 which I have on my desktop (8 on laptop).
    I use Vim for an editor, tcsh for my shell (doesn't work as
    well on 8 as it does on 7) and ActiveState Perl which is
    easy to install and add modules to using PPM. I try to use
    native Windows ported UNIX utilities (rather than emulated
    ones as in CygWin). I don't do much, if any, compiling - just
    never managed to properly set up a decent compiler/development
    environment (and not about to pay for Windoze MSVC) - so I just
    use pre-compiled AS Perl for everything.
    $Bill, Sep 22, 2013
    #5
    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:
    286
    Todd Wade
    Jul 5, 2008
  2. George Mpouras

    Re: utilities in perl

    George Mpouras, Sep 21, 2013, in forum: Perl Misc
    Replies:
    5
    Views:
    182
    J. Gleixner
    Sep 26, 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:
    174
    Charles DeRykus
    Sep 27, 2013
Loading...

Share This Page