{"id":103,"date":"2009-06-19T14:22:20","date_gmt":"2009-06-19T14:22:20","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2009\/06\/using_config_files_in_catalyst-shtml\/"},"modified":"2009-06-19T14:22:20","modified_gmt":"2009-06-19T14:22:20","slug":"using_config_files_in_catalyst-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/","title":{"rendered":"Using Config Files In Catalyst"},"content":{"rendered":"<p>\nI&#8217;ve been doing quite a bit of work with the <a href=\"http:\/\/www.catalystframework.org\/\">Catalyst MVC framework<\/a> lately.\n<\/p>\n<p>\nMoving from development to live, the config system has really shown it&#8217;s usefulness. Instead of hardcoding values, we can simply move them out to the site&#8217;s config file and access these value programatically. It means we can run the same codebase on multiple machines and just tweak the config to pick up things like different database connection strings, cache settings etc.\n<\/p>\n<p>\nI like to have my config file in <a href=\"http:\/\/search.cpan.org\/dist\/Config-General\/\">Config::General<\/a> format as it&#8217;s similar to Apache&#8217;s config files, but Catalyst can also handle config files in INI, JSON, Perl, XML or YAML, so you can use whatever you are most comfortable with.\n<\/p>\n<p>\nLet&#8217;s have a look at a few examples. We&#8217;ll assume the Catalyst Application is called MyApp. This means we&#8217;ll have a perl module in our <var>lib<\/var> directory called MyApp.pm, and a config file called <var>myapp.conf<\/var> in the root directory.\n<\/p>\n<p>\nMyApp.pm contains all the default values, but you can override these with myapp.conf. myapp.conf always takes priority.\n<\/p>\n<p>\nLet&#8217;s create a simple string to say where our application is running. In MyApp.pm, in the <code>__PACKAGE__-&gt;config<\/code> we add an entry to the hash like this&#8230;\n<\/p>\n<div class=\"code\"><code><br \/>\nservername =&gt; 'dev',<br \/>\n<\/code><\/div>\n<p>\nNow when we run our application, we can access this value using the following code&#8230;\n<\/p>\n<div class=\"code\"><code><br \/>\n$c-&gt;config-&gt;{servername};<br \/>\n<\/code><\/div>\n<p>\nThis will return &#8220;dev&#8221;. We can override this in the conf file, so in myapp.conf we can add&#8230;\n<\/p>\n<div class=\"code\"><code><br \/>\nservername production<br \/>\n<\/code><\/div>\n<p>\nNow when we run our application code, we&#8217;ll see &#8220;production&#8221; instead of &#8220;dev&#8221;.\n<\/p>\n<p>\nA more practical use of the config file is to move out the database connection details. Let&#8217;s assume we have a simple MySQL based model in our lib\/Model directory called Model::MyApp that handles our database work. We can override database connection details stored here in myapp.conf using something like this&#8230;\n<\/p>\n<div class=\"code\"><code>&lt;\"Model::MyApp\"&gt;<br \/>\nconnect_info dbi:mysql:myapp<br \/>\nconnect_info www-rw<br \/>\nconnect_info password<br \/>\n&lt;connect_info&gt;<br \/>\nAutoCommit 1<br \/>\n&lt;\/connect_info&gt;<br \/>\n&lt;\/\"Model::MyApp\"&gt;<br \/>\n<\/code><\/div>\n<p>\nNow when we run the application, the connection details we entered in our conf file will be used instead. This is very useful as it means we don&#8217;t have to alter our codebase when we move the application to different servers that may have different databases. It&#8217;s also good for keeping dev, staging and live seperate as all that&#8217;s needed is a change to one config file.\n<\/p>\n<p>\nFor more information on how Catalyst can use a config file, have a look at<br \/>\n<a href=\"http:\/\/search.cpan.org\/~mramberg\/Catalyst-Plugin-ConfigLoader-0.23\/\">Catalyst::Plugin::ConfigLoader<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing quite a bit of work with the Catalyst MVC framework lately. Moving from development to live, the config system has really shown it&#8217;s usefulness. Instead of hardcoding values, we can simply move them out to the site&#8217;s config file and access these value programatically. It means we can run the same codebase &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using Config Files In Catalyst&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[13,47,75],"class_list":["post-103","post","type-post","status-publish","format-standard","hentry","category-dev","tag-catalyst","tag-perl","tag-web-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using Config Files In Catalyst - Robert Price<\/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:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Config Files In Catalyst - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been doing quite a bit of work with the Catalyst MVC framework lately. Moving from development to live, the config system has really shown it&#8217;s usefulness. Instead of hardcoding values, we can simply move them out to the site&#8217;s config file and access these value programatically. It means we can run the same codebase &hellip; Continue reading &quot;Using Config Files In Catalyst&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2009-06-19T14:22:20+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using Config Files In Catalyst\",\"datePublished\":\"2009-06-19T14:22:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/\"},\"wordCount\":408,\"keywords\":[\"Catalyst\",\"Perl\",\"Web Development\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/\",\"name\":\"Using Config Files In Catalyst - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2009-06-19T14:22:20+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using_config_files_in_catalyst-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Config Files In Catalyst\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using Config Files In Catalyst - Robert Price","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:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Using Config Files In Catalyst - Robert Price","og_description":"I&#8217;ve been doing quite a bit of work with the Catalyst MVC framework lately. Moving from development to live, the config system has really shown it&#8217;s usefulness. Instead of hardcoding values, we can simply move them out to the site&#8217;s config file and access these value programatically. It means we can run the same codebase &hellip; Continue reading \"Using Config Files In Catalyst\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/","og_site_name":"Robert Price","article_published_time":"2009-06-19T14:22:20+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using Config Files In Catalyst","datePublished":"2009-06-19T14:22:20+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/"},"wordCount":408,"keywords":["Catalyst","Perl","Web Development"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/","name":"Using Config Files In Catalyst - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2009-06-19T14:22:20+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using_config_files_in_catalyst-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using Config Files In Catalyst"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=103"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/103\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}