anurag has asked for the wisdom of the Perl Monks concerning the following question:

How to read contents of a file without openning it and print them??

  • Comment on How to read contents of a file without opening it and print the contents

Replies are listed 'Best First'.
Re: How to read contents of a file without opening it and print the contents
by Corion (Patriarch) on Jun 10, 2011 at 13:35 UTC

    open is a prerequisite to reading any file by name. You cannot do it otherwise.

    You can pipe the content of a file into your program. That input will then be available in the STDIN filehandle.

Re: How to read contents of a file without opening it and print the contents
by moritz (Cardinal) on Jun 10, 2011 at 13:59 UTC
Re: How to read contents of a file without opening it and print the contents
by zek152 (Pilgrim) on Jun 10, 2011 at 13:38 UTC

    I hand you a flash drive. Tell me what information is stored on it without inserting it into any usb host. Also physically opening the drive is not allowed.

    This is not meant to be mean nor to insult. I am simply trying to give a more physical analogy to help you see what you are asking and how it sounds.

    You have to "open" a file to read the contents.

    open FILE, "filename" or die "could not open filename"; while(<FILE>) { print $_; } close FILE;

      Based on that analogy, couldn't you have your script open and read /dev/hda1, parse the filesystem, find the file and read its data?

        That's why I put the word open in quotes.

        In your example I would say that "opening" /dev/hda1 means that you are "opening" a set of which the file is a subset. "Opening" a set implies that you have "opened" all subsets. (note the continued use of quotation marks)

        Because I know that someone will point out that opening a set does not imply opening all subsets (example: a box filled with other boxes) I will clarify that in this case I am refering to sets that contain only singletons (in this case bytes or blocks) as elements.

        Update: fixed moderate typo.

Re: How to read contents of a file without opening it and print the contents
by ambrus (Abbot) on Jun 10, 2011 at 14:02 UTC
Re: How to read contents of a file without opening it and print the contents
by SuicideJunkie (Vicar) on Jun 10, 2011 at 14:11 UTC

    A good point was brought up in the CB... perhaps this question is being asked because the OP tried to open a file and it failed.

    If that is the case, use

    open my $fh, '<', $name or die "The open failed because of '$!'\n";

    ... then fix the problem that is reported.

      I am attempting to fulfill an assignment that involves searching the file using regular expressions. "write a script called xxx.pl. This script should read the contents of /httpd/conf/httpd.conf without opening it and print out every line that contains /cgi-bin." So How does one "read a file without opening it?"
        It took you 4 years to return to this thread? Wow!

        Could it be that your assignment is rather HTTP than regex related?

        but even then .... a very stupid question.

        update

        maybe someone thought that using shell redirections do not open: $content=`<filename` ?

        update

        or $content=`cat filename` ?

        but even then .... a very stupid question!

        Cheers Rolf

        PS: Je suis Charlie!

Re: How to read contents of a file without opening it and print the contents
by Anonymous Monk on May 24, 2019 at 21:04 UTC
    # Pass file name as an argument on command line while (<>) { print; }