Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Creating a bash script "on the fly"

by 1nickt (Canon)
on Mar 26, 2021 at 20:22 UTC ( [id://11130396]=note: print w/replies, xml ) Need Help??


in reply to Creating a bash script "on the fly"

This kind of black box module?

use v5.12; use strict; use warnings; package MyClass { use Moo; has _map => ( is => 'lazy', builder => sub { # however you get your list put that here return { 'file1' => { type => 'file' }, 'somedir/file2' => { type => 'file' }, 'dir1' => { type => 'dir' }, 'somedir/dir2' => { type => 'dir' }, 'link1' => { type => 'link', target => 'somedi +r/file1' }, 'link2' => { type => 'link', target => 'otherd +ir/file3' }, }; }, ); sub files { my $self = shift; return [ grep { $self->_map->{$_}{type} eq 'file' } keys %{ $s +elf->_map } ]; } sub directories { my $self = shift; return [ grep { $self->_map->{$_}{type} eq 'dir' } keys %{ $se +lf->_map } ]; } sub links { my $self = shift; my @links = grep { $self->_map->{$_}{type} eq 'link' } keys %{ + $self->_map }; return [ map { { $_ => $self->_map->{$_}{target} } } @links ]; } }; my $obj = MyClass->new; say for @{ $obj->directories };
Output:
dir1 somedir/dir2

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Creating a bash script "on the fly"
by kennethk (Abbot) on Mar 26, 2021 at 20:36 UTC
    Glad to see someone write a bunch of the code that needed writing, but that I didn't have time to write. As a piece of feedback, you should probably use File::Spec to do some path manipulation, particularly since you need to create parent directories before their children.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Or Path::Tiny which will let you create the directories (otherwise you'll probably File::Path for make_path which works akin to mkdir -p to call yourself).

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

Re^2: Creating a bash script "on the fly"
by ovedpo15 (Pilgrim) on Mar 27, 2021 at 10:22 UTC
    Hi!
    Thank you for suggestion. My question focus on how to build the builder part. I have bunch of paths. How do I know for example if there is a link in the path? Even if I will iterate over each path and check it, how do I know the link? realpath won't work for links such as: a/b -> c/d -> e/f
      I have bunch of paths. How do I know for example if there is a link in the path?

      You won't know from just looking at the string. You'll have to check the filesystem, and for each pathname break it down into its components, using e.g. -l on each one. You can break down a filename using e.g. splitdir from File::Spec, or IMO a little easier, Path::Class. Maybe something like:

      use warnings; use strict; use Path::Class qw/file dir/; my $file = file('/tmp/foolink/bar/quz'); my $prev; while (1) { die "doesn't exist: $file" unless -e $file; print $file, " is a ", -l $file ? 'link to '.readlink($file) : -f $file ? 'file' : -d _ ? 'dir' : 'special', "\n"; $prev = $file; $file = $file->parent; last if $prev eq $file; } __END__ /tmp/foolink/bar/quz is a file /tmp/foolink/bar is a dir /tmp/foolink is a link to foo /tmp is a dir / is a dir

      Update: Added a check to the above code to make sure the file exists in the first place.

        Oh I see! I like it! Is there a way to iterate the other way around (from the start of the path to the end instead from the end to the start)? It will be easier to insert the paths into the hash that way.
        Also, does it know how to handle with multi-links? For example a/b -> c/d -> e/f? In case it's a link, should I iterate until I get the actual path?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130396]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-24 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found