Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Hi Monks could you pls help Perl dummy user

by Anonymous Monk
on Dec 17, 2014 at 01:15 UTC ( [id://1110584]=note: print w/replies, xml ) Need Help??


in reply to Hi Monks could you pls help Perl dummy user

recursion doesn't work i don't know why?

Because your read_dir is not safe to use for recursion - $dir and all the other variables in the sub are global. You need to use lexical variables, for example my $dh instead of DH, to make them local to the blocks they are defined in. Using warnings and strict (use warnings; use strict;) will help you with this and many other potential problems.

without fancy module

Why? File::Find is in the core.

use File::Find; find( sub { return unless -r && -f; my $size = -s; print "$File::Find::name $size\n" if $size > 51200; }, $path );

Replies are listed 'Best First'.
Re^2: Hi Monks could you pls help Perl dummy user
by LanX (Saint) on Dec 17, 2014 at 01:51 UTC
    > Using warnings and strict (use warnings; use strict;) will help you with this and many other potential problems.

    unfortunately do neither use strict nor use warnings complain about bareword filehandles, and nowadays I wonder why.

    They don't have an explicit package name and localizing with a glob local *FH isn't trivial.

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

      That's output for my program that doesn't go into any directories. Do you know why?

      yura@yura:~$ ls -a . .bashrc dir_tree .face +.goutputstream-51G14W .ICEauthority .mixxx Pictures .pu +lse-cookie Templates .xsession-errors.old .. .cache .dmrc .gconf +.goutputstream-F8CZ4W .java Mixxx .profile .Sk +ype Videos .adobe .compiz Documents .gnome2 +.goutputstream-TBVW4W java_progs module project .ss +h .viminfo .apport-ignore.xml .config Downloads .gnome2_private +.goutputstream-Z89K4W .local .mozilla project_final .sw +t workspace .bash_history .dbus .eclipse .goutputstream-11KV4W +.gstreamer-0.10 .macromedia Music Public sys +err.log .Xauthority .bash_logout Desktop examples.desktop .goutputstream-1UX14W +.gtk-bookmarks .mission-control perl .pulse sys +out.log .xsession-errors yura@yura:~$ perl dir_tree .ICEauthority 33170 module 433 project_final 4515 dir_tree 704 .profile 675 project 4486 sysout.log 19807 examples.desktop 8445 syserr.log 2350 .pulse-cookie 256 .xsession-errors.old 828 .viminfo 14093 .bash_history 17965 .face 9447 .bashrc 3637 .bash_logout 220 yura@yura:~$
Re^2: Hi Monks could you pls help Perl dummy user
by sibyurik (Novice) on Dec 17, 2014 at 01:40 UTC

    I used strict and warnings but it doesn't go into any other directories other than current. Thus recursion doesn't work correctly i guess. Could you pls help me tofix it now?

    #!/usr/bin/perl # #Author: Yury Sibirski #Name: users_home_dir #Date: 16 December 2014 #Purpose: This program analyze the directory structure of a Linux disk + and identify any files larger than 500 kbytes # use strict; use warnings; my $path = shift || '.'; read_dir($path); sub read_dir { my $dir = shift; opendir (my $DH, $dir) or die "Couldn't open current directory +: $!"; while (my $file = readdir($DH)) {#print "$file\n"; if ($file eq "." or $file eq "..") { next; } elsif (-z $file) { next; } elsif (-r $file and -f $file) { my $size = -s $file; print $file," ",$size, "\n" if $size > 200; } elsif (-d $file) { read_dir($file); } } closedir $DH; }
      works for me!

      Cheers Rolf

      (addicted to the Perl Programming Language and ☆☆☆☆ :)

        It checks all your directories and prints files that bigger than 200?
Re^2: Hi Monks could you pls help Perl dummy user
by jellisii2 (Hermit) on Dec 17, 2014 at 14:41 UTC
    This 1000 times. Beginners guide to File::Find has a nice simple explanation on how to use it. The code that you have written and commented out is potentially dangerous as it will blindly follow symlinks. Since you're on Linux, this is a very real concern.
Re^2: Hi Monks could you pls help Perl dummy user
by sibyurik (Novice) on Dec 17, 2014 at 02:11 UTC

    It gives me an error like no &wanted subroutine given at /usr/share/perl/5.18/File/Find.pm line 1073. NO IDEA WHY?

    #!/usr/bin/perl use File::Find; use strict; use warnings; my $path = shift || '.'; find($path); find( sub { return unless -r && -f; my $size = -s; print "$File::Find::name $size\n" if $size > 51200; }, $path )

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found