{"id":18841,"date":"2022-11-02T03:29:21","date_gmt":"2022-11-01T21:59:21","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/"},"modified":"2022-11-02T03:29:21","modified_gmt":"2022-11-01T21:59:21","slug":"solved-perl-script-cant-use-tiefile","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/","title":{"rendered":"[Solved] Perl Script can&#8217;t use Tie::File"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-27196586\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"27196586\" data-parentid=\"27190016\" data-score=\"2\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Your program is frankly a bit of a mess. You seem to be trying things in the hope that they work but without any real reasoning.<\/p>\n<p>I have refactored your code to do what I think you want below. Here are the main changes I have made<\/p>\n<ul>\n<li>\n<p>You must <em>always<\/em> add <code>use strict<\/code> and <code>use warnings<\/code> to <em>every<\/em> Perl program you write, and declare all your variables with <code>my<\/code> as close as possible to their first point of use. Those simple measures alone will save you from a lot of simple errors that you will otherwise overlook<\/p>\n<\/li>\n<li>\n<p>You don&#8217;t need <code>Tie::Array<\/code> or <code>Cwd<\/code>. They are irrelevant to this program<\/p>\n<\/li>\n<li>\n<p>Your <code>tie<\/code> statement needs a <em>string<\/em> as the second parameter, so you need to use <code>'Tie::File'<\/code> instead of <code>Tie::File<\/code><\/p>\n<\/li>\n<li>\n<p>Your output file <code>Trace.txt<\/code> will be found by the <code>&lt;*.txt&gt;<\/code> glob, so unless you take measures to specifically exclude it your program will copy trim the first and last lines and copy the contents of that file to itself. In my program I have simply checked in the <code>for<\/code> loop whether the current file name is <code>Trace.txt<\/code> and skipped it if so<\/p>\n<\/li>\n<li>\n<p>There is no point in accumulating the data in a buffer <code>$buff<\/code>. You may as well just write the data to the file as you encounter it<\/p>\n<\/li>\n<li>\n<p>The lines in the tied array <code>@lines<\/code> have no trailing newline, so you will presumably want to add one when you write to the file<\/p>\n<\/li>\n<li>\n<p>As has been discussed in the comments, you are using <code>Tie::FILE<\/code> and <code>TIE::File<\/code> as well as the correct <code>Tie::File<\/code>. And you have written <code>use Tie::File<\/code> (and its variations) four times in total. Sure it doesn&#8217;t stop the program from working, but it is a major indication of foggy thinking, and that you are just statements around in the hope that they make your program work<\/p>\n<\/li>\n<li>\n<p>Using <code>delete<\/code> on anything other than the <em>last<\/em> element of an array just sets that element to <code>undef<\/code>: it doesn&#8217;t delete it, and all that happens in the tied file is that the text is removed leaving just a newline. You need to use <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/perldoc.perl.org\/functions\/splice.html\"><code>splice<\/code><\/a> instead<\/p>\n<\/li>\n<li>\n<p>Separating your files into the first, the last, and the rest is unnecessary and makes your code illegible. In my program below I have used a single loop that removes the first line of the file unless it&#8217;s the first fil, and removes the last line of the file unless it&#8217;s the last file. It&#8217;s far easier to read that way<\/p>\n<\/li>\n<li>\n<p>Lastly, I&#8217;m not at all sure that you want to remove the first and last lines from the <em>existing<\/em> files, or if you just want all the data copied to your output file <em>except<\/em> those lines. I have written my program according to your specification, but bear in mind that the files will get shorter by two lines <em>every time you run it<\/em>, and that probably isn&#8217;t the effect you want. If you have a different requirement and can&#8217;t see how to modify the code to achieve it then please ask another question.<\/p>\n<\/li>\n<\/ul>\n<p>I hope this helps you.<\/p>\n<pre><code>use strict;\nuse warnings;\n\nuse Tie::File;\n\nmy @files = grep -f, glob '*.txt';\n\nmy $all_filename=\"Trace.txt\";\nopen my $out_fh, '&gt;', $all_filename or die qq{Unable to open \"$all_filename\" for output: $!};\n\nfor my $i ( 0 .. $#files ) {\n\n  my $file = $files[$i];\n  next if $file eq $all_filename;\n\n  print \"Opening $file\\n\";\n\n  tie my @lines, 'Tie::File', $file or die qq{Can't update \"$file\": $!};\n  splice @lines,  0, 1 unless $i == 0;\n  splice @lines, -1, 1 unless $i == $#files;\n\n  print $out_fh \"$_\\n\" for @lines;\n}\n\nclose $out_fh;\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Perl Script can&#8217;t use Tie::File <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Your program is frankly a bit of a mess. You seem to be trying things in the hope that they work but without any real reasoning. I have refactored your code to do what I think you want below. Here are the main changes I have made You must always add use strict and &#8230; <a title=\"[Solved] Perl Script can&#8217;t use Tie::File\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\" aria-label=\"More on [Solved] Perl Script can&#8217;t use Tie::File\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[442,443],"class_list":["post-18841","post","type-post","status-publish","format-standard","hentry","category-solved","tag-perl","tag-unix"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Perl Script can&#039;t use Tie::File - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Perl Script can&#039;t use Tie::File - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Your program is frankly a bit of a mess. You seem to be trying things in the hope that they work but without any real reasoning. I have refactored your code to do what I think you want below. Here are the main changes I have made You must always add use strict and ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-01T21:59:21+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Perl Script can&#8217;t use Tie::File\",\"datePublished\":\"2022-11-01T21:59:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\"},\"wordCount\":515,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"perl\",\"unix\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\",\"name\":\"[Solved] Perl Script can't use Tie::File - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-01T21:59:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Perl Script can&#8217;t use Tie::File\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Perl Script can't use Tie::File - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Perl Script can't use Tie::File - JassWeb","og_description":"[ad_1] Your program is frankly a bit of a mess. You seem to be trying things in the hope that they work but without any real reasoning. I have refactored your code to do what I think you want below. Here are the main changes I have made You must always add use strict and ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/","og_site_name":"JassWeb","article_published_time":"2022-11-01T21:59:21+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Perl Script can&#8217;t use Tie::File","datePublished":"2022-11-01T21:59:21+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/"},"wordCount":515,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["perl","unix"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/","url":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/","name":"[Solved] Perl Script can't use Tie::File - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-01T21:59:21+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-perl-script-cant-use-tiefile\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Perl Script can&#8217;t use Tie::File"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=18841"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/18841\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=18841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=18841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=18841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}