Hi Monks!
I'm using the Graph module to create a graph which represents the filesystem. Each vertex contains the canonical path. They also contain an attribute "type" with their type (file, dir or link).
Given a path, I fill the graph like so:
sub add_path { my ($self,$path) = @_; my $canonical_path = get_canonical_path($path); my @subpaths = splitdir($canonical_path); for my $index (0..$#subpaths) { next if (@subpaths[$index] eq ""); # Ignore empty strings my $parent_path = abs_path(catdir(@subpaths[0..($index-1)])); my $current_path = catdir($parent_path,@subpaths[$index]); my $original_path_flag = ($index eq $#subpaths); next if ($graph->has_vertex($current_path)); if (-l $current_path) { my @resloved_links = resolve_symlink($current_path); my $target_path = $resloved_links[1]; $self->add_path($target_path); $graph->add_edge($parent_path,$current_path); $graph->add_edge($current_path,$target_path); $graph->set_vertex_attributes($current_path, { "type" => " +link", "target" => $target_path, "original" => $original_path_flag }) +; } elsif (-f $current_path) { $graph->add_edge($parent_path,$current_path); $graph->set_vertex_attributes($current_path, { "type" => " +file", "original" => $original_path_flag }); } elsif (-d $current_path) { $graph->add_edge($parent_path,$current_path); $graph->set_vertex_attributes($current_path, { "type" => " +dir", "original" => $original_path_flag }); } } }
Basically I split find the canonical path of the given path, split it and create a vertex of each subpath, with an edge to the next one. The $original_path_flag represents full paths (you will understand later why I need it).
So you get a graph with vertices - some are dirs, some are files and some are links. If it's a dir, then it has zero or more "children", if it's a file, then it does not have "children" and if it's a link, then it has only one child. But note, that all the vertices contain canonical paths, without links in the way. Now, once I fill the graph, I want to mark *original* paths that match one of the regex expressions, with keyword "mark". My current code:
sub run_rexes { my ($self,$rexes_aref) = @_; foreach my $vertex ($graph->unique_vertices) { next unless ($graph->get_vertex_attribute($vertex, 'original') +); if (is_regex_matches($vertex,$rexes_aref)) { $graph->set_vertex_attribute($vertex, "marked", 1); } } }
Where is_regex_matches returns true if $vertex matches one of the regex expressions. It works good but I want to add support to links in the regex expressions. Since $graph->unique_vertices returns the vertices and each one contains only the absolute paths, then running a regex with link on the way, will fail. Just to make it more clear, consider:
/a/b/c/file /p -> /a/b/c
So a regex like so: ^/a/b/c/file$ will work, but ^/p/file$ will not work.
Is it possible to suggest a solution for it?

In reply to Graph which represents the filesystem by ovedpo15

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.